package com.qboot.core.config;

import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Swagger 配置
 *
 * @author Tellsea
 * @date 2022/11/25
 */
@Configuration
@EnableOpenApi
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {

    @Bean("startDocket")
    public Docket startDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("启动模块：qboot-admin")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qboot.start.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("generateDocket")
    public Docket generateDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("代码生成模块：qboot-extend-generate")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qboot.generate.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("systemDocket")
    public Docket systemDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("系统模块：qboot-system")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qboot.system.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("monitorDocket")
    public Docket monitorDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("监控模块：qboot-extend-monitor")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qboot.monitor.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("commonDocket")
    public Docket commonDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("常用模块：qboot-common")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qboot.common.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("ossLocalDocket")
    public Docket ossLocalDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("对象存储本地：qboot-extend-oss-local")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qboot.osslocal.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("jobQuartzDocket")
    public Docket jobQuartzDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("定时任务Quartz：qboot-extend-quartz")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qboot.quartz.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean("fileStorageDocket")
    public Docket fileStorageDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("qboot-extend-file-storage")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qboot.filestorage.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("开发文档在线接口")
                .contact(new Contact("Tellsea", "https://gitee.com/tellsea/qboot", "3210054449@qq.com"))
                .version("1.0.0")
                .build();
    }

    /**
     * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
     **/
    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }

    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }
}
